﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.SignalR;
using Microsoft.Net.Http.Headers;
using NetCoreCMS.Common;
using NetCoreCMS.IServices;
using StackExchange.Redis;

namespace NetCoreCMS.Web.Areas.Web.Controllers
{
    public class UserCenterController : UserController
    {
        public string action = string.Empty;
        public string curr_login_ip = string.Empty;
        public string pre_login_ip = string.Empty;
        public string pre_login_time = string.Empty;
        public int total_order;
        public int total_msg;
        public IUser_login_logService _user_login_log { get; set; }
        public IOrdersService _orders { get; set; }
        public IUser_messageService _user_message { get; set; }
        public IUser_oauth_appService _user_oauth_app { get; set; }
        public IHubContext<NoticeHub> hubContext { get; set; }
        public IActionResult Index()
        {
            action = Request.Query["act"];
            if (string.IsNullOrEmpty(action))
                action = "index";
            ViewBag.inviteList = _users.get_user_invite_list(0, "user_name='" + userModel.user_name + "'");
            //获得最后登录日志
            var dt = _user_login_log.GetList(2, "where user_name='" + userModel.user_name + "'", "id desc");
            if (dt.Count == 2)
            {
                curr_login_ip = dt[0].login_ip;
                pre_login_ip = dt[1].login_ip;
                pre_login_time = dt[1].login_time.ToString();
            }
            else if (dt.Count == 1)
            {
                curr_login_ip = dt[0].login_ip;
            }
            //未完成订单
            total_order = _orders.RecordCount("where user_name='" + userModel.user_name + "' and status<3");
            //未读短信息
            total_msg = _user_message.RecordCount("where accept_user_name='" + userModel.user_name + "' and is_read=0");

            //退出登录==========================================================
            if (action == "exit")
            {
                //清险Session
                HttpContext.Session.Remove(CMSKeys.SESSION_USER_INFO);
                //清除Cookies
                Response.Cookies.Delete(CMSKeys.COOKIE_USER_NAME_REMEMBER);
                Response.Cookies.Delete(CMSKeys.COOKIE_USER_PWD_REMEMBER);
                Response.Cookies.Delete("UserName");
                Response.Cookies.Delete("Password");
                //自动登录,跳转URL
                return Redirect("/Web/Login/Index");
            }
            ViewBag.action = action;
            if(action=="index")
            {
                #region wx
                string redisName = Request.Host.Host.ToLower().Replace(".", "_") + "_";
                var app = _user_oauth_app.GetFirst();
                string shareUrl = config.weburl + Request.PathBase + Request.Path + Request.QueryString;
                var acmodel = WXTokenHelper.GetAccessToken(redisName, app, shareUrl);
                ViewBag.appid = app.oauth_app_id;
                ViewBag.ts = acmodel.ts;
                ViewBag.noncestr = acmodel.noncestr;
                ViewBag.jsapi_ticket = acmodel.jsapi_ticket;
                ViewBag.signature = acmodel.signature;
                #endregion
            }
            //signalR
            this.hubContext.Clients.All.SendAsync("OnMsg", DateTime.Now, userModel.user_name,CMSRequest.GetClientUserIp(HttpContext));
            return View();
        }
        [HttpPost]
        public ActionResult EditGPS(float longitude, float latitude)
        {
            _users.UpdateField(userModel.id, "longitude=" + longitude + ",latitude=" + latitude);
            return Content("ok");
        }
    }
}